<?php
/**
 * admin service file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2019/08/23
 * Time: 15:42
 */

namespace Modules\Admin\Service;

use HiCommon\Helper;
use HiCommon\Repository\DiscoverRepository;
use HiCommon\Repository\DiscoverZanRepository;
use HiCommon\Repository\DiscoverAppriseRepository;

class DiscoverService
{

    /**
     *  获取朋友圈列表数据
     *  hinq 2019 10 28
     *
     **/
    public function get_list(array $arr_search)
    {
        try {
            $arr_search['act'] = 'admin';
            $arr_option['with'] = ['shop_user'];
            //countZan   点赞数统计:只统计朋友圈的点赞数  朋友圈下的评论点赞数不统计
            $arr_option['field'] = ['id','title','img','status','pid','created_at','remark','repairstore_id','author_id'];
            $arr_list = DiscoverRepository::get_list((array)$arr_option, (array)$arr_search);
            $arr_list->appends($arr_search);//添加链接参数
            $arr_list = list_page_data($arr_list, $arr_search);
            return hello_success('成功', $arr_list );
        } catch (\Exception $exception) {
            return ['state' => 1, 'msg' => $exception->getMessage()];
        }
    }



    /**
     *  获取朋友圈列表数据
     *  hinq 2019 10 28
     *
     **/
    public function get_discover_list(array $arr_search)
    {
        try {

            $arr_option['where'] = ['status' => 1];
            $arr_option['with'] = ['shop_user', 'admin'];
            //countZan   点赞数统计:只统计朋友圈的点赞数  朋友圈下的评论点赞数不统计
            $arr_option['withCount'] = ['countZan', 'countPing'];
            $arr_list = DiscoverRepository::get_list((array)$arr_option, (array)$arr_search);
            $arr_list->appends($arr_search);//添加链接参数
            $arr_list = list_page_data($arr_list, $arr_search);
            return hello_success('成功', $arr_list);
        } catch (\Exception $exception) {
            return ['state' => 1, 'msg' => $exception->getMessage()];
        }
    }


    /**
     *    验证表单数据
     *    hinq 2019 10 28
     *
     **/
    public function checkdata(array $arr_input)
    {

        if (empty($arr_input['title'])) {
            return ['state' => 1, 'msg' => '标题不能为空', 'data' => [], 'error' => ''];
        }
        if (strlen($arr_input['title']) < 5) {
            return ['state' => 1, 'msg' => '标题最少5个字符', 'data' => [], 'error' => ''];
        }
        if (empty($arr_input['content'])) {
            return ['state' => 1, 'msg' => '内容不能为空', 'data' => [], 'error' => ''];
        }
        preg_match_all('/<img.*?src="(.*?)".*?>/is', $arr_input['content'], $imgs);

        if (count($imgs[1]) == 0) {
            return ['state' => 1, 'msg' => '请上传图片', 'data' => [], 'error' => ''];
        } else {
            $arr_input['img'] = $imgs[1][0];
        }
        if (empty($arr_input['pid'])) {
            return ['state' => 1, 'msg' => '请选择类型', 'data' => [], 'error' => ''];
        }
//        if (empty($arr_input['issue'])) {
//            return ['state' => 1, 'msg' => '请选择发布类型', 'data' => [], 'error' => ''];
//        }
        return $arr_input;
    }


    /**
     *    添加朋友圈数据
     *  hinq 2019 10 28
     *
     **/
    public function store(array $arr_input)
    {
        try {
            $arr_input = self::checkdata((array)$arr_input);
            if ($arr_input['state'] == 1) {
                return $arr_input;
            }
            $arr_input['look_num'] = empty( $arr_input['look_num']) ? '0' : $arr_input['look_num'];
            $arr_input['type'] = 2;  // 2： 后台发布    1：前端发布已删除
            $arr_input['status'] = 1;
            $arr_input['issue'] = 1;
            $arr_input['repairstore_id'] = 1;
            $arr_input['author_id'] = admin_user_id();
            $res = DiscoverRepository::add_one((array)$arr_input);
            if (empty($res)) {
                return hello_error('添加失败');
            }
            return hello_success('添加成功', $res);
        } catch (\Exception $exception) {
            return ['state' => 1, 'msg' => $exception->getMessage()];
        }
    }


    /**
     *    删除朋友圈
     *    hinq 2019 10 28
     *
     **/
    public function del(int $nId)
    {
        try {
            if (empty($nId)) {
                return hello_error('参数错误');
            }
            //验证当前需要删除的数据是否可以删除
            $where = ['type' => 1, 'author_id' => admin_user_id()];
            $count = DiscoverRepository::get_count_by_where((array)$where);
            if ($count == 0) {
                return hello_error('您没有权限删除');
            }
            $res = DiscoverRepository::delete_one((int)$nId);
            if (empty($res)) {
                return hello_error('删除失败');
            }
            return hello_success('删除成功', $res);
        } catch (\Exception $exception) {
            return ['state' => 1, 'msg' => $exception->getMessage()];
        }

    }

    /**
     *  获取朋友圈详细信息
     *  hinq 2019 10 29
     *
     **/
    public function detail(array $arr_search)
    {
        try {
            if ($arr_search['act'] == 'ping') { //评论
                //获取点赞人的列表
                $arr_option['where'] = ['discover_id' => $arr_search['id'], 'lever' => 1];
                $arr_option['field'] = ['id', 'user_id', 'created_at', 'discover_id', 'content', 'zan_num'];
                $arr_option['with'] = ['to_user'];
                $arr_list = DiscoverAppriseRepository::get_list((array)$arr_option);

            } else if ($arr_search['act'] == 'zan') {  //点赞
                //获取点赞人的列表
                $arr_option['where'] = ['link_id' => $arr_search['id'], 'type' => 1];
                $arr_option['field'] = ['id', 'user_id', 'created_at'];
                $arr_option['with'] = ['to_user'];
                $arr_list = DiscoverZanRepository::get_list((array)$arr_option);
            }
            $arr_list->appends($arr_search);//添加链接参数
            $arr_list = list_page_data($arr_list, $arr_search);

            foreach ($arr_list['data'] as $key => &$item) {
                $key = substr($item['user_id'], -1);
                if (empty($item['to_user'])) {
                    //游客默认头像
                    $item['to_user'] = [
                        "nickname" => '游客' . $item['user_id'],
                        "head_img" => [
                            "full_src" => "pages/imgs/you/you" . $key . ".png"
                        ]
                    ];
                }
            }

            return hello_success('成功', $arr_list);
        } catch (\Exception $exception) {
            return ['state' => 1, 'msg' => $exception->getMessage()];
        }
    }

    /**
     *  获取指定的数据
     *  hinq 2019 12 05
     *
     **/
    public function get_one(int $nId)
    {
        try {
            if (empty($nId)) {
                return hello_error('参数错误');
            }
            $arr_info = DiscoverRepository::get_one((int)$nId);
            if (empty($arr_info->first())) {
                return hello_error('参数错误');
            }
            return hello_success('ok', $arr_info->toArray());
        } catch (\Exception $exception) {
            return ['state' => 1, 'msg' => $exception->getMessage()];
        }
    }


    /**
     *  编辑指定的朋友去数据
     *  hinq 2019 12 05
     *
     **/
    public function update(array $arr_input)
    {
        try {
            $temp = self::checkdata((array)$arr_input);

            unset($temp['_token']);
            $admin_id = admin_user_id();
            //验证当前数据是否是操作员的
            $count = DiscoverRepository::get_count_by_where((array)['author_id' => $admin_id, 'id' => $arr_input['id'] , 'type' => 1 ]);
            if ($count == 0) {
                return hello_error('您没有权限更新数据');
            }
            $res = DiscoverRepository::edit_one((array)$temp);
            if (empty($res)) {
                return hello_error('编辑失败');
            }
            return hello_success('编辑成功');
        } catch (\Exception $exception) {
            return ['state' => 1, 'msg' => $exception->getMessage()];
        }
    }

    /**
     *  平台审核商户添加的朋友圈信息
     *  hinq 2020 6 11
     *
     **/
    public function shenhe( array $arr_input ){
        try{
            if( empty( $arr_input['content'] ) ){
                return hello_error('审核信息不能为空');
            }
            if( empty( $arr_input['id'] ) || empty( $arr_input['status'] ) ){
                return hello_error('参数错误');
            }
            $arr_option['field'] = ['id','status'];
            $info = DiscoverRepository::get_one( $arr_input['id'] , (array) $arr_option );
            if( empty( $info->first() ) ){
                return hello_error('参数错误');
            }
            if( $info['status'] == 1 ){  //待审核和驳回审核的数据都可以重新审核
                return hello_error('操作失败，请刷新页面');
            }
            $data['status'] = $arr_input['status'];
            $data['id'] = $arr_input['id'];
            $data['remark'] = $arr_input['content'];
            $res = DiscoverRepository::edit_one( $data );
            if( empty( $res) ){
                return hello_error('操作失败');
            }
            return hello_success( '操作成功');
        }catch (\Exception $exception){
            return hello_error( $exception->getMessage());
        }

    }


}